/**
 * 态势对象关系管理 API
 */
import { apiAdminGet, apiAdminPost, apiAdminPut, apiAdminDelete, apiGet, apiPost } from './base'

/**
 * 获取关系列表
 * @param {Object} params - 查询参数
 * @param {number} params.page - 页码
 * @param {number} params.page_size - 每页数量
 * @param {string} params.source_object_id - 源对象ID筛选
 * @param {string} params.target_object_id - 目标对象ID筛选
 * @param {string} params.relationship_type - 关系类型筛选
 * @param {number} params.relationship_level - 关系层级筛选
 * @returns {Promise}
 */
export function getRelationships(params = {}) {
  const queryParams = new URLSearchParams()

  if (params.page) queryParams.append('page', params.page)
  if (params.page_size) queryParams.append('page_size', params.page_size)
  if (params.source_object_id) queryParams.append('source_object_id', params.source_object_id)
  if (params.target_object_id) queryParams.append('target_object_id', params.target_object_id)
  if (params.relationship_type) queryParams.append('relationship_type', params.relationship_type)
  if (params.relationship_level) queryParams.append('relationship_level', params.relationship_level)

  return apiGet(`/api/situation/relationships?${queryParams.toString()}`)
}

/**
 * 获取单个关系详情
 * @param {string} relationshipId - 关系ID
 * @returns {Promise}
 */
export function getRelationship(relationshipId) {
  return apiGet(`/api/situation/relationships/${relationshipId}`)
}

/**
 * 创建关系
 * @param {Object} data - 关系数据
 * @param {string} data.source_object_id - 源对象ID
 * @param {string} data.target_object_id - 目标对象ID
 * @param {string} data.relationship_type - 关系类型
 * @param {Object} data.properties - 关系属性
 * @param {string} data.description - 关系描述
 * @returns {Promise}
 */
export function createRelationship(data) {
  return apiAdminPost(`/api/situation/relationships`, data)
}

/**
 * 更新关系
 * @param {string} relationshipId - 关系ID
 * @param {Object} data - 更新数据
 * @returns {Promise}
 */
export function updateRelationship(relationshipId, data) {
  return apiAdminPut(`/api/situation/relationships/${relationshipId}`, data)
}

/**
 * 删除关系
 * @param {string} relationshipId - 关系ID
 * @returns {Promise}
 */
export function deleteRelationship(relationshipId) {
  return apiAdminDelete(`/api/situation/relationships/${relationshipId}`)
}

/**
 * 获取关系树(递归查询子关系)
 * @param {string} rootObjectId - 根对象ID
 * @param {number} maxLevel - 最大层级(1-5)
 * @returns {Promise}
 */
export function getRelationshipTree(rootObjectId, maxLevel = 5) {
  return apiGet(`/api/situation/relationships/tree/${rootObjectId}?max_level=${maxLevel}`)
}

/**
 * 检测循环依赖
 * @param {Object} data - 检测数据
 * @param {string} data.source_object_id - 源对象ID
 * @param {string} data.target_object_id - 目标对象ID
 * @param {string} data.relationship_type - 关系类型
 * @returns {Promise}
 */
export function validateCycle(data) {
  return apiPost(`/api/situation/relationships/validate-cycle`, data)
}

/**
 * 导出态势对象到知识图谱
 * @param {Object} data - 导出数据
 * @param {Array<string>} data.object_ids - 要导出的对象ID列表(可选,为空则导出全部)
 * @param {boolean} data.include_relationships - 是否包含关系(默认true)
 * @param {boolean} data.clear_existing - 是否清除已存在的数据(默认false)
 * @returns {Promise}
 */
export function exportToKnowledgeGraph(data) {
  return apiAdminPost(`/api/situation/export-to-graph`, data)
}
